package com.alibaba.yinan.graph;

import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import org.jgrapht.graph.DefaultEdge;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * @ClassName GraphVisualizer
 * @Author mengke
 * @create 2025/8/3 14:44
 * @Description
 */
@Component
public class GraphVisualizer {

    private final Graph graph = new SingleGraph("Road Network");

    public void renderGraph(Set<String> nodes, Set<DefaultEdge> edges, org.jgrapht.Graph<String, DefaultEdge> jGraphTGraph) {
        graph.clear();

        // 添加节点
        for (String node : nodes) {
            Node n = graph.addNode(node);
            n.setAttribute("ui.label", node);
        }

        // 添加边
        int i = 0;
        for (DefaultEdge edge : edges) {
            String src = jGraphTGraph.getEdgeSource(edge);
            String tgt = jGraphTGraph.getEdgeTarget(edge);
            String id = src + "-" + tgt;  // 改成有意义的边ID
            if (graph.getEdge(id) == null && graph.getNode(src) != null && graph.getNode(tgt) != null) {
                graph.addEdge(id, src, tgt);
            }
        }
        graph.display();
    }

    public void highlightPath(List<String> path) {
        for (int i = 0; i < path.size(); i++) {
            Node node = graph.getNode(path.get(i));
            if (node != null) {
                node.setAttribute("ui.style", "fill-color: red;");
            }
        }
    }

    public void renderGraphAsImage(Set<String> allNodes, Set<DefaultEdge> allEdges, List<String> path, org.jgrapht.Graph<String, DefaultEdge> graph, String fullPath) {
    }
}
